package CommonGui;

import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Utils {
	public static void hideColumns(JTable tbl, int[] cols) {
		/**
		 * Hiding columns of table.
		 * 
		 * @author Alex
		 * @since 10.12.12
		 * @param tbl
		 *            Reference to table
		 * @param cols
		 *            array of columns indecies to hide
		 */
		for (int i : cols) {
			tbl.getColumnModel().getColumn(i).setMinWidth(0);
			tbl.getColumnModel().getColumn(i).setMaxWidth(0);
			tbl.getColumnModel().getColumn(i).setPreferredWidth(0);
		}
	}
	
	public static void updateRowHeights(JTable table)
	{
	    try
	    {
	        for (int row = 0; row < table.getRowCount(); row++)
	        {
	            int rowHeight = table.getRowHeight();

	            for (int column = 0; column < table.getColumnCount(); column++)
	            {
	                Component comp = table.prepareRenderer(table.getCellRenderer(row, column), row, column);
	                rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
	            }

	            table.setRowHeight(row, rowHeight);
	        }
	    }
	    catch(ClassCastException e) {}
	}
	
	public static void clearTable(JTable tbl) {
		/**
		 * Clearing table data from MODEL
		 * 
		 * @author Alex
		 * @since 10.12.12
		 * @param tbl
		 *            Reference to table to remove row from
		 */
		while (tbl.getModel().getRowCount() > 0)
			((DefaultTableModel) (tbl.getModel())).removeRow(0);
	}
}
